/**
 * Created by lyf on 2017/6/20.
 */


'use strict'

let node_ssh = require('node-ssh');
const serviceConfig = require('../conf/service.json');
let ssh = new node_ssh();

module.exports = function depolySeckillManager(seckillManager , zookeeper ,redis , kongRedis ) {
    return new Promise (async function(resolve,reject){
        console.log(seckillManager);
        for ( let i = 0 ; i < seckillManager.length ; i++){
            await ssh.connect({
                host: seckillManager[i][1],
                username: serviceConfig.user ,
                password: serviceConfig.password
            });
            const result = await ssh.execCommand( `docker run -d ` +
                ` -p 3000:3000 ` +
                ` -e SK_REDIS="redis://:${serviceConfig.password}@${redis[0][0]}" ` +
                ` -e SK_KAFKA_ZOOKEEPER="${zookeeper[0][0]}:2181" ` +
                ` -e KONG_REDIS_HOST="${kongRedis[0][0]}" ` +
                ` -e KONG_REDIS_PASSWORD="${serviceConfig.password}" ` +
                ` -e SERVER_IP="$(ifconfig eth0 | grep inet | awk '{print $2}')" ` +
                ` -e ZOOKEEPER_ADDR="${zookeeper[0][0]}:2181" ` +
                ` --name seckillmanager-service ` +
                ` foreveross/seckillmanager-service `
            );
            ssh.dispose();
            //result.stdout ? resolve() : reject ();

            console.log(`seckillManager:${seckillManager[i][1]}`);
            console.log("-----------------------------");
            console.log(result.stdout);
            console.log(result.stderr);
            console.log("-----------------------------");
            resolve();

        }
    })

}